package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author dongdongxiaoyuanmao
 * @since 2025-03-06
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {

    @Autowired
    private ICategoryService categoryService;

    @Override
    public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryBrand) {
        return baseMapper.selectCategoryBrandList(categoryBrand);
    }

    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        Long categoryId = categoryBrand.getCategoryId();
        Long brandId = categoryBrand.getBrandId();
        //根据categoryId和brandId查询记录是否存在
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrand::getCategoryId, categoryId)
                .eq(CategoryBrand::getBrandId, brandId);
        Long count = baseMapper.selectCount(queryWrapper);
        //如果记录大于0，说明有记录，不能再添加
        if (count > 0) {
            throw new ServiceException("该分类下商品品牌已经存在");
        }
        return baseMapper.insert(categoryBrand);
    }

    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        //在category_brand数据库中通过id找记录
        Long categoryBrandId = categoryBrand.getId();
        CategoryBrand categoryBrandFromDB = baseMapper.selectById(categoryBrandId);
        //如果我要修改的记录没有发生改变，直接返回
        //也就是说如果我的categoryId和brandId没有发生改变，直接返回
        if (categoryBrandFromDB.getCategoryId().equals(categoryBrand.getCategoryId())
                && categoryBrandFromDB.getBrandId().equals(categoryBrand.getBrandId())) {
            //代表记录没有发生任何变化，所以也就不需要修改
            return 1;
        }

        //如果我录入的记录发生了改变，那么就需要去数据库中查看是否有我录入的记录
        //如果有，那么就抛出异常，说明该品牌已经存在
        Long categoryId = categoryBrand.getCategoryId();
        Long brandId = categoryBrand.getBrandId();
        //根据categoryId和brandId查询记录是否存在
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CategoryBrand::getCategoryId, categoryId)
                .eq(CategoryBrand::getBrandId, brandId);
        Long count = baseMapper.selectCount(queryWrapper);
        //如果记录大于0，说明有记录，不能再添加
        if (count > 0) {
            throw new ServiceException("该分类下商品品牌已经存在");
        }

        //如果没有，那么修改数据库中的记录
        return baseMapper.updateById(categoryBrand);

    }

    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = baseMapper.selectById(id);
        Long categoryId = categoryBrand.getCategoryId();

        //我们需要把id的list集合封装进去，这样我们就可以知道三级id列表的值是多少
        //达到级联的效果
        List<Long> categoryIdList = categoryService.getAllCategoryIdList(categoryId);
        categoryBrand.setCategoryIdList(categoryIdList);

        return categoryBrand;
    }

    @Override
    public List<Brand> selectBrandListByCategoryId(Long categoryId) {
        return baseMapper.selectBrandListByCategoryId(categoryId);
    }
}
